import type { Handle } from "@sveltejs/kit";
import { sequence } from "@sveltejs/kit/hooks";
import { handleErrorWithSentry, sentryHandle } from "@sentry/sveltekit";
import * as Sentry from "@sentry/sveltekit";
import { locale } from "svelte-i18n";

Sentry.init({
  dsn: "https://19ca433cd275a680d5e35857bc289cb0@o83602.ingest.us.sentry.io/4507827211862016",
  tracesSampleRate: 1.0,

  // uncomment the line below to enable Spotlight (https://spotlightjs.com)
  // spotlight: import.meta.env.DEV,
});

// If you have custom handlers, make sure to place them after `sentryHandle()` in the `sequence` function.
export const handle: Handle = sequence(
  sentryHandle(),
  async ({ event, resolve }) => {
    const lang = event.request.headers.get("accept-language")?.split(",")[0];
    if (lang) {
      locale.set(lang);
    }
    return resolve(event);
  },
);

// If you have a custom error handler, pass it to `handleErrorWithSentry`
export const handleError = handleErrorWithSentry();
